package com.sumer.psychology.common.domain.model;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * User: zhenguang.zhu
 * Date: 2014/8/11
 * Time: 9:20
 */
public abstract class AbstractDomainEvent implements DomainEvent {
    private int eventVersion;
    private Date occurredOn;

    protected AbstractDomainEvent() {
        setEventVersion(1);
        setOccurredOn(new Date());
    }

    @Override
    public int eventVersion() {
        return this.eventVersion;
    }

    @Override
    public Date occurredOn() {
        return this.occurredOn;
    }

    protected void setEventVersion(int eventVersion) {
        this.eventVersion = eventVersion;
    }

    protected void setOccurredOn(Date occurredOn) {
        this.occurredOn = occurredOn;
    }

    /**
     * 获得author的详细信息
     *
     * @param author author
     * @return author
     */
    protected Author authorFrom(Author author) {
        if (StringUtils.isNoneBlank(author.identity(), author.name())) {
            return author;
        }
        return CommonDomainRegistry.userService().authorFrom(author);
    }
}
